#include "WallRebound.h"

WallRebound::WallRebound(const Particle& pIn, const Flag& pFlag, const shared_ptr<WallGeometry> wallGeometry0,
	const ImpingementParameter& ImpingePara_in)
{
	if (pFlag.impingement) // impingement event
	{
		//Elastic rebound
		pOut = pIn;
		vector<double> wallNormal;
		if (ImpingePara_in.nearestBCFaceType == 0) // bottom wall
		{
			wallNormal = (*wallGeometry0).normal_bot;
		}
		else if (ImpingePara_in.nearestBCFaceType == 1) // top wall
		{
			wallNormal = (*wallGeometry0).normal_top;
		}
		else
		{
			assert(false && "Undefined boundary for impingement!");
		}
		pOut.V[0] = (pIn.V[0] - DotProduct(pIn.V, wallNormal) * wallNormal[0]) - DotProduct(pIn.V, wallNormal) * wallNormal[0]; // rotation operation, transfer from wall reference to global reference
		pOut.V[1] = (pIn.V[1] - DotProduct(pIn.V, wallNormal) * wallNormal[1]) - DotProduct(pIn.V, wallNormal) * wallNormal[1]; // rotation operation, transfer from wall reference to global reference
	}
	else
	{
		pOut = pIn;
	}
}

double WallRebound::DotProduct(vector<double> v1, vector<double> v2)
{
	// This function is only valid for 2d
	return v1[0] * v2[0] + v1[1] * v2[1];
}